/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.davial.mgwt.client.activities.patient.detail;

import it.davial.mgwt.client.ClientFactory;
import it.davial.mgwt.client.Patient;
import it.davial.mgwt.client.activities.BaseActivity;
import it.davial.mgwt.client.activities.patient.PatientView;
import it.davial.mgwt.client.event.ActionEvent;
import it.davial.mgwt.client.event.ActionNames;
import it.davial.mgwt.client.service.DbServiceAsync;

import java.util.List;
import java.util.logging.Logger;

import org.soul.mgwt.client.widget.CsEditableCellList;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.HTML;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedHandler;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class PatientDetailActivity extends BaseActivity {
	private final static String NEW_PATIENT = "Inserisci paziente";
	private final static HTML TITLE = new HTML(NEW_PATIENT);

	private static final Logger logger = Logger.getLogger("PatientActivity");

	/**
	 * Used to obtain views, eventBus, placeController.
	 * Alternatively, could be injected via GIN.
	 */
	private ClientFactory clientFactory;
	private Patient patient;

	/**
	 * Sample property.
	 */

	public PatientDetailActivity(ClientFactory clientFactory) {
		super(clientFactory.getPatientView(), "nav");
		this.clientFactory = clientFactory;
	}

	public void setPatient(Patient patient){
		this.patient = patient;
		
	}
	
	@Override
	public void start(AcceptsOneWidget containerWidget, final EventBus eventBus) {
		logger.info("starting PatientActivity");
		final PatientDetailView view = clientFactory.getPatientDetailView();
		view.getBackbuttonText().setText("Home");

		if ( patient != null){
			TITLE.setText(patient.getName().concat(" ").concat(patient.getSurname()));
			
		} else
			TITLE.setText(NEW_PATIENT);
		
		view.setPatient(patient);
		view.getHeader().setCenterWidget(TITLE);
		view.getMainButtonText().setText("Nav");
		view.getSaveButtonText().setText("Save");
	
		addHandlerRegistration(view.getBackbutton().addTapHandler(new TapHandler() {

			@Override
			public void onTap(TapEvent event) {
				ActionEvent.fire(eventBus, ActionNames.BACK);
			}
		}));

		addHandlerRegistration(view.getBack().addTapHandler(new TapHandler() {

			@Override
			public void onTap(TapEvent event) {
				ActionEvent.fire(eventBus, ActionNames.BACK);
			}
		}));

		addHandlerRegistration(view.getSave().addTapHandler(new TapHandler() {

			@Override
			public void onTap(TapEvent event) {
				try {
					savePatient(view);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}));

			containerWidget.setWidget(view.asWidget());
	}

/*	@Override
	public String mayStop() {
		return "Please hold on. This activity is stopping.";
	}
*/
	/**
	 * @see PatientDetailView.Presenter#goTo(Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
	
	private void savePatient(final PatientDetailView view) throws Exception{
		view.getHeader().setCenterWidget(progressHeader);
		Patient p = view.getPatientForm().createPatient();
		DbServiceAsync.Util.getInstance().savePatient(p, new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				 logger.info("save patient success, result=" + result);
				 view.getHeader().setCenterWidget(TITLE);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				logger.info("save patient failure, " + caught);
				view.getHeader().setCenterWidget(TITLE);
			}
		});
	
	}
	
/*	@Override
	public void onStop() {
		PatientDetailView view = clientFactory.getPatientDetailView();
		view.setPatient(null);
	}
*/
}
